<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin:  0 auto;
        }
        .box {
            width: 400px;
            height: 400px;
            background-color: purple;
            text-align: center;
            position: relative;
            overflow: hidden;
        }
        .f {
            position: absolute;
            background-color: yellow;
            opacity: 0.5;
            width: 100px;
            height: 100px;
        }

    </style>
</head>
<body>
    <div class="box">
        <div class="f"></div>
    </div>
    <script>
        var box = document.querySelector('.box');
        var f = document.querySelector('.f');
        box.addEventListener('mousemove', function(e) {
            var y = (e.clientY - box.offsetTop - f.offsetHeight / 2) + 'px';
            var x = (e.clientX - box.offsetLeft - f.offsetWidth / 2) + 'px';
            f.style.top = y;
            f.style.left = x;
        });
    </script>
</body>
</html>